/*
 * TrackModelPane.java
 * This file is used to instatiate the Track Model GUI
 * Author: Daniel Hartman
 * Date Created: 2012-11-24
 * Date Last Updated: 2012-12-09
 */

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


/**
 * @author Dan
 *
 */
public class TrackModelPane {

        private JFrame frame;
        static String blockNumber = new String("Block: ");
        static String type = new String("Type: ");
        static String state = new String("State: ");
        static String speed = new String("Speed Limit: ");
        static String length = new String("Length: ");
        static String grade = new String("Grade: ");
        static JLabel blockLabel;
        static JLabel typeLabel;
        static JLabel stateLabel;
        static JLabel speedLabel;
        static JLabel lengthLabel;
        static JLabel gradeLabel;
        private final ImageIcon normalIcon = new ImageIcon("normal.gif");
        private final ImageIcon verticalIcon = new ImageIcon("vertical.gif");
        private final ImageIcon topleftIcon = new ImageIcon("topleft.gif");
        private final ImageIcon toprightIcon = new ImageIcon("topright.gif");
        private final ImageIcon bottomrightIcon = new ImageIcon("bottomright.gif");
        private final ImageIcon bottomleftIcon = new ImageIcon("bottomleft.gif");
        private final ImageIcon uprightIcon = new ImageIcon("upright.gif");
        private final ImageIcon upleftIcon = new ImageIcon("upleft.gif");
        private final ImageIcon downrightIcon = new ImageIcon("downright.gif");
        private final ImageIcon crossingIcon = new ImageIcon("crossing.gif");
        private final ImageIcon stationIcon = new ImageIcon("station.gif");





        public static JButton[] red = new JButton[77];
        public static JButton[] green = new JButton[151];
        JButton[] fill = new JButton[200];
        private JPanel redLayoutPanel;
        private JPanel greenLayoutPanel;


        /**
         * Launch the application.
         * @throws IOException
         * @throws InterruptedException
         */
        public static void main(String[] args) throws IOException, InterruptedException {
                //TrackModel tm = new TrackModel();
                
                EventQueue.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                                try {
                                        //TrackModelPane window = new TrackModelPane();
                                	TrackModel.build();
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                });
        }

        /**
         * Create the application.
         */
        public TrackModelPane() {
                initialize();
        }

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
                frame = new JFrame("Track Model");
                frame.setBounds(100, 100, 980, 200);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setLayout(new GridLayout(0, 1, 0, 10));

                JPanel statusPanel = new JPanel();
                frame.getContentPane().add(statusPanel);
                statusPanel.setLayout(new GridLayout(0, 6, 0, 0));

                JPanel panel = new JPanel();
                FlowLayout flowLayout = (FlowLayout) panel.getLayout();
                flowLayout.setAlignment(FlowLayout.LEFT);
                statusPanel.add(panel);

                blockLabel = new JLabel(blockNumber);
                panel.add(blockLabel);
                blockLabel.setBounds(new Rectangle(0, 0, 70, 0));

                JPanel panel_1 = new JPanel();
                FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
                flowLayout_1.setAlignment(FlowLayout.LEFT);
                statusPanel.add(panel_1);

                typeLabel = new JLabel(type);
                panel_1.add(typeLabel);

                JPanel panel_2 = new JPanel();
                FlowLayout flowLayout_2 = (FlowLayout) panel_2.getLayout();
                flowLayout_2.setAlignment(FlowLayout.LEFT);
                statusPanel.add(panel_2);

                stateLabel = new JLabel(state);
                panel_2.add(stateLabel);

                JPanel panel_3 = new JPanel();
                FlowLayout flowLayout_3 = (FlowLayout) panel_3.getLayout();
                flowLayout_3.setAlignment(FlowLayout.LEFT);
                statusPanel.add(panel_3);

                speedLabel = new JLabel(speed);
                panel_3.add(speedLabel);

                JPanel panel_4 = new JPanel();
                FlowLayout flowLayout_4 = (FlowLayout) panel_4.getLayout();
                flowLayout_4.setAlignment(FlowLayout.LEFT);
                statusPanel.add(panel_4);

                lengthLabel = new JLabel(length);
                panel_4.add(lengthLabel);

                JPanel panel_5 = new JPanel();
                FlowLayout flowLayout_5 = (FlowLayout) panel_5.getLayout();
                flowLayout_5.setAlignment(FlowLayout.LEFT);
                statusPanel.add(panel_5);

                gradeLabel = new JLabel(grade);
                panel_5.add(gradeLabel);

                redLayoutPanel = new JPanel();
                frame.getContentPane().add(redLayoutPanel);
                redLayoutPanel.setLayout(new GridLayout(0, 52, 0, 0));

                greenLayoutPanel = new JPanel();
                frame.getContentPane().add(greenLayoutPanel);
                greenLayoutPanel.setLayout(new GridLayout(0, 86, 0, 0));

                //build graphical redline
                for(int i = 1; i <= 76; i++){
                        switch(TrackModel.red[i].type)
                        {
                        case 0:
                        case 2:
                                switch(i)
                                {
                                case 1:
                                                TrackModel.red[i].icon = uprightIcon;
                                        red[i] = new JButton(uprightIcon);
                                        break;
                                case 7:
                                                TrackModel.red[i].icon = bottomleftIcon;
                                        red[i] = new JButton(bottomleftIcon);
                                        break;
                                case 8:
                                        TrackModel.red[i].icon = topleftIcon;
                                        red[i] = new JButton(topleftIcon);
                                        break;
                                case 59:
                                        TrackModel.red[i].icon = toprightIcon;
                                        red[i] = new JButton(toprightIcon);
                                        break;
                                case 60:
                                case 67:
                                case 72:
                                        TrackModel.red[i].icon = bottomrightIcon;
                                        red[i] = new JButton(bottomrightIcon);
                                        break;
                                case 66:
                                case 71:
                                case 76:
                                        TrackModel.red[i].icon = upleftIcon;
                                        red[i] = new JButton(upleftIcon);
                                        break;
                                default:
                                        TrackModel.red[i].icon =normalIcon;
                                        red[i] = new JButton(normalIcon);
                                }
                                break;
                        case 1:
                                TrackModel.red[i].icon = stationIcon;
                                red[i] = new JButton(stationIcon);
                                break;
                        case 3:
                                TrackModel.red[i].icon = crossingIcon;
                                red[i] = new JButton(crossingIcon);
                                break;
                        }
                        red[i].setBackground(Color.WHITE);
                        red[i].addActionListener(TrackModel.red[i]);
                }


                //build graphical greenline
                for(int i = 1; i <= 150; i++)
                {
                        switch(TrackModel.green[i].type)
                        {
                        case 0:
                        case 2:
                                switch(i)
                                {
                                case 1:
                                        TrackModel.green[i].icon = uprightIcon;
                                        green[i] = new JButton(uprightIcon);
                                        break;
                                case 5:
                                case 149:
                                        TrackModel.green[i].icon = bottomleftIcon;
                                        green[i] = new JButton(bottomleftIcon);
                                        break;
                                case 6:
                                case 93:
                                case 100:
                                case 101:
                                case 150:
                                        TrackModel.green[i].icon = verticalIcon;
                                        green[i] = new JButton(verticalIcon);
                                        break;
                                case 7:
                                        TrackModel.green[i].icon = topleftIcon;
                                        green[i] = new JButton(topleftIcon);
                                        break;
                                case 92:
                                        TrackModel.green[i].icon = toprightIcon;
                                        green[i] = new JButton(toprightIcon);
                                        break;
                                case 99:
                                        TrackModel.green[i].icon = upleftIcon;
                                        green[i] = new JButton(upleftIcon);
                                        break;
                                case 94:
                                case 102:
                                        TrackModel.green[i].icon = bottomrightIcon;
                                        green[i] = new JButton(bottomrightIcon);
                                        break;
                                default: TrackModel.green[i].icon = normalIcon;
                                                green[i] = new JButton(normalIcon);
                                }
                                break;
                        case 1:
                                TrackModel.green[i].icon = stationIcon;
                                green[i] = new JButton(stationIcon);
                                break;
                        case 3:
                                TrackModel.green[i].icon = crossingIcon;
                                green[i] = new JButton(crossingIcon);
                                break;
                        }
                        green[i].setBackground(Color.WHITE);
                        green[i].addActionListener(TrackModel.green[i]);
                }

                for(int i = 7; i <= 92; i++)
                {
                        greenLayoutPanel.add(green[i]);
                }
                greenLayoutPanel.add(green[6]);
                for(int i = 28; i < 31; i++){
                        fill[i] = new JButton("");
                        fill[i].setEnabled(false);
                        greenLayoutPanel.add(fill[i]);
                }
                greenLayoutPanel.add(green[1]);
                for(int i = 31; i < 48; i++){
                        fill[i] = new JButton("");
                        fill[i].setEnabled(false);
                        greenLayoutPanel.add(fill[i]);
                }
                greenLayoutPanel.add(green[150]);
                for(int i = 48; i < 94; i++){
                        fill[i] = new JButton("");
                        fill[i].setEnabled(false);
                        greenLayoutPanel.add(fill[i]);
                }
                greenLayoutPanel.add(green[101]);
                for(int i = 94; i < 103; i++){
                        fill[i] = new JButton("");
                        fill[i].setEnabled(false);
                        greenLayoutPanel.add(fill[i]);
                }
                greenLayoutPanel.add(green[100]);
                for(int i = 103; i < 108; i++){
                        fill[i] = new JButton("");
                        fill[i].setEnabled(false);
                        greenLayoutPanel.add(fill[i]);
                }
                greenLayoutPanel.add(green[93]);
                for(int i = 5; i > 1; i--)
                {
                        greenLayoutPanel.add(green[i]);
                }
                for(int i = 108; i < 126; i++){
                        fill[i] = new JButton("");
                        fill[i].setEnabled(false);
                        greenLayoutPanel.add(fill[i]);
                }
                for(int i = 149; i > 101; i--)
                {
                        greenLayoutPanel.add(green[i]);
                }
                for(int i = 126; i < 136; i++){
                        fill[i] = new JButton("");
                        fill[i].setEnabled(false);
                        greenLayoutPanel.add(fill[i]);
                }
                for(int i = 99; i > 93; i--)
                {
                        greenLayoutPanel.add(green[i]);
                }




                for(int i = 8; i <= 59; i++){
                        redLayoutPanel.add(red[i]);
                }
                for(int i = 7; i>0; i--){
                        redLayoutPanel.add(red[i]);
                }
                for(int i = 0; i < 13; i++){
                        fill[i] = new JButton("");
                        fill[i].setEnabled(false);
                        redLayoutPanel.add(fill[i]);
                }
                for(int i = 76; i > 71; i--){
                        redLayoutPanel.add(red[i]);
                }
                for(int i = 13; i < 19; i++){
                        fill[i] = new JButton("");
                        fill[i].setEnabled(false);
                        redLayoutPanel.add(fill[i]);
                }
                for(int i = 71; i > 66; i--){
                        redLayoutPanel.add(red[i]);
                }
                for(int i = 19; i < 28; i++){
                        fill[i] = new JButton("");
                        fill[i].setEnabled(false);
                        redLayoutPanel.add(fill[i]);
                }
                for(int i = 66; i > 59; i--){
                        redLayoutPanel.add(red[i]);
                }
                frame.setVisible(true);
        }
}
